package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.Chat;
import org.apache.ibatis.annotations.*;

public interface ChatMapper {
    @Delete({
        "delete from chat",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into chat (id, type, ",
        "uid, anchor_uid, begin_time, ",
        "update_time, end_time, ",
        "minutes, status, ",
        "task_id, task_update_time, ",
        "task_scan_times, task_scan_msg)",
        "values (#{id,jdbcType=BIGINT}, #{type,jdbcType=TINYINT}, ",
        "#{uid,jdbcType=BIGINT}, #{anchorUid,jdbcType=BIGINT}, #{beginTime,jdbcType=TIMESTAMP}, ",
        "#{updateTime,jdbcType=TIMESTAMP}, #{endTime,jdbcType=TIMESTAMP}, ",
        "#{minutes,jdbcType=INTEGER}, #{status,jdbcType=TINYINT}, ",
        "#{taskId,jdbcType=BIGINT}, #{taskUpdateTime,jdbcType=TIMESTAMP}, ",
        "#{taskScanTimes,jdbcType=INTEGER}, #{taskScanMsg,jdbcType=VARCHAR})"
    })
    int insert(Chat record);

    int insertSelective(Chat record);

    @Select({
        "select",
        "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status, ",
        "task_id, task_update_time, task_scan_times, task_scan_msg",
        "from chat",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    Chat selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Chat record);

    @Update({
        "update chat",
        "set type = #{type,jdbcType=TINYINT},",
          "uid = #{uid,jdbcType=BIGINT},",
          "anchor_uid = #{anchorUid,jdbcType=BIGINT},",
          "begin_time = #{beginTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "end_time = #{endTime,jdbcType=TIMESTAMP},",
          "minutes = #{minutes,jdbcType=INTEGER},",
          "status = #{status,jdbcType=TINYINT},",
          "task_id = #{taskId,jdbcType=BIGINT},",
          "task_update_time = #{taskUpdateTime,jdbcType=TIMESTAMP},",
          "task_scan_times = #{taskScanTimes,jdbcType=INTEGER},",
          "task_scan_msg = #{taskScanMsg,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Chat record);

    /***********以下为自定义方法************************/

    @Select({
            "select",
            "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status",
            "from chat",
            "where id = #{id,jdbcType=BIGINT} and status = #{status,jdbcType=TINYINT} for update"
    })
    @ResultMap("BaseResultMap")
    Chat selectByIdAndStatusForUpdate(@Param("id") Long id, @Param("status") Integer status);

    @Select({
            "select",
            "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status",
            "from chat",
            "where id = #{id,jdbcType=BIGINT} for update"
    })
    @ResultMap("BaseResultMap")
    Chat selectByIdForUpdate(@Param("id") Long id);

    @Select({
            "select",
            "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status",
            "from chat",
            "where uid = #{uid,jdbcType=BIGINT} and anchor_uid = #{anchorUid,jdbcType=BIGINT} and status = #{status,jdbcType=TINYINT} for update"
    })
    @ResultMap("BaseResultMap")
    Chat selectByUidForUpdate(@Param("uid") Long uid, @Param("anchorUid") Long anchorUid, @Param("status") Integer status);

    @Select({
            "select",
            "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status",
            "from chat",
            "where uid = #{uid,jdbcType=BIGINT} and status in (1, 3) for update"
    })
    @ResultMap("BaseResultMap")
    Chat selectActivityChatByUidForUpdate(@Param("uid") Long uid);

    @Select({
            "select",
            "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status",
            "from chat",
            "where (uid = #{uid,jdbcType=BIGINT} or anchor_uid = #{uid,jdbcType=BIGINT}) and status = 3 and type = 2 order by id desc limit 1 for update"
    })
    @ResultMap("BaseResultMap")
    Chat selectActivityRandomChatByUid(@Param("uid") Long uid);

    @Select({
            "select",
            "id, type, uid, anchor_uid, begin_time, update_time, end_time, minutes, status",
            "from chat",
            "where anchor_uid = #{uid,jdbcType=BIGINT} and status = 3 and type = 2 order by id desc limit 1 for update"
    })
    @ResultMap("BaseResultMap")
    Chat selectActivityRandomChatByAnchorUid(@Param("uid") Long uid);
}